package test;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

import utils.XmlConverUtil;

/**
 *map和list的相互嵌套 同名节点会放进list中
 */
public class demo {

	/**
	 * @param args
	 * @throws DocumentException
	 */
	public static void main(String[] argv) throws DocumentException {
		System.out.println(sysData());
		// Iterator it = sysData().iterator();
		// while(it.hasNext()){
		//			
		// }

	}

	public static List sysData() throws DocumentException {
		String xmlStr = xmlString();
		try {
			StringReader sr = new StringReader(xmlStr);
			InputSource is = new InputSource(sr);
			Document doc = (new SAXBuilder()).build(is);
			// String filterstr =
			// "HTTPServer,Title,X-Frame-Options,UncommonHeaders,Meta-Refresh-Redirect,Country,www.baidu.com,200,X-UA-Compatible,Mobile-Website,IP";
			String filterstr = "";
			List m = getResultMap(doc, filterstr);
			return m;
			// System.out.println(m);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static List getResultMap(Document doc, String filterStr) {
		List maplist = new ArrayList();
		if (doc == null)
			return maplist;
		Element root = doc.getRootElement();
		for (Iterator iterator = root.getChildren().iterator(); iterator
				.hasNext();) {
			Map<String, Object> map = new HashMap<String, Object>();
			Element e = (Element) iterator.next();
			List list = e.getChildren();
			if (list.size() > 0) {
				map.put(e.getName(), getChildMap(e, filterStr));
			} else{
				map.put(e.getName(), e.getText());
			   //System.out.println("map00----------:"+map);
			}
			maplist.add(map);
		}
		//System.out.println("maplist----------:"+maplist);
		return maplist;
	}

	@SuppressWarnings("unchecked")
	public static Map getChildMap(Element e, String filterStr) {
		Map map = new HashMap();
		List list = e.getChildren();
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Element iter = (Element) list.get(i);
				List mapList = new ArrayList();
				if (iter.getChildren().size() > 0) {
					if (validateName(iter, filterStr)) {// 过滤在filterStr中包含的值
						Map m = getChildMap(iter, filterStr);
						if (map.get(iter.getName()) != null) {
							Object obj = map.get(iter.getName());
//							System.out.println("obj_--------"+obj);
							if (!obj.getClass().getName().equals(
									"java.util.ArrayList")) {
								mapList = new ArrayList();
								mapList.add(obj);
								mapList.add(m);
							}
							if (obj.getClass().getName().equals(
									"java.util.ArrayList")) {
								mapList = (List) obj;
								mapList.add(m);
							}
							map.put(iter.getName(), mapList);
						} else{
							map.put(iter.getName(), m);
						}
					}
					//System.out.println("map1----------:"+map);
				} else {
					if (validateName(iter, filterStr)) {// 过滤在filterStr中包含的值
						if (map.get(iter.getName()) != null) {
							Object obj = map.get(iter.getName());
							if (!obj.getClass().getName().equals(
									"java.util.ArrayList")) {
								mapList = new ArrayList();
								mapList.add(obj);
								mapList.add(iter.getText());
								//System.out.println();
							}
							if (obj.getClass().getName().equals(
									"java.util.ArrayList")) {
								mapList = (List) obj;
								mapList.add(iter.getText());
							}
							map.put(iter.getName(), mapList);
						} else{
							map.put(iter.getName(), iter.getText());
						}
					}

				}
			}
		} else{
			map.put(e.getName(), e.getText());
		}
		//System.out.println("map2-------------"+map);
		return map;
	}

	/** 判断含有子节点的name子节点的值或单个节点的值是否在filterContent中存在 */
	public static boolean validateName(Element iter, String filterContent) {
		List children = iter.getChildren();
		if (children.size() > 0) {
			for (int i = 0; i < children.size(); i++) {
				Element child = (Element) children.get(i);
				if ("name".equals(child.getName())
						&& filterContent.contains(child.getText())) {
					return false;
				}
			}
		} else {
			if (filterContent.contains(iter.getText())) {
				return false;
			}
		}
		return true;
	}

	public static String xmlString() throws DocumentException {
		SAXReader sr = new SAXReader();// 获取读取xml的对象。
		org.dom4j.Document doc = sr.read("src/Entities.xml");// 得到xml所在位置。然后开始读取。并将数据放入doc中
		String str = XmlConverUtil.doc2String(doc);
		return str;
	}
}
